import 'package:flutter/cupertino.dart';
import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:ls_work/widgets/title_value_widget.dart';
import 'package:oktoast/oktoast.dart';

class MeterReadingBottomInputWidget extends StatefulWidget {
  final String title;
  final String hint;
  final String btnText;
  final Function(MeterReadingData reading)? onSubmit;
  final CommonInputType keyboard;

  String content = "";

  MeterReadingBottomInputWidget(
      {this.title = "请输入",
        this.hint = "请输入",
        this.btnText = "确定",
        this.onSubmit,
        this.content = "",
        dynamic typeId,
        this.keyboard = CommonInputType.multiline}) {
    if(typeId==null){
      selectType = typeList.first;
    }else{
      selectType = typeList.firstWhere((element) => element.pickId==typeId);
    }
  }

  PickerDataWithIdModel? selectType;

  var typeList = [
    PickerDataWithIdModel(pickName: "正常抄表", pickId: "1"),
    PickerDataWithIdModel(pickName: "请假离院抄表", pickId: "2"),
    PickerDataWithIdModel(pickName: "请假回院抄表", pickId: "3"),
    PickerDataWithIdModel(pickName: "变更入房间抄表", pickId: "4"),
    PickerDataWithIdModel(pickName: "变更出房间", pickId: "5"),
    PickerDataWithIdModel(pickName: "结算抄表", pickId: "6"),
    PickerDataWithIdModel(pickName: "入住抄表", pickId: "7"),
  ];

  @override
  State<StatefulWidget> createState() =>_BottomChartInputWidget();


}

class _BottomChartInputWidget extends State<MeterReadingBottomInputWidget>{

  var format = ["yyyy", '-', 'mm', '-', 'dd', ' ', 'HH', ':', 'nn', ':', 'ss'];

  var selectTime = DateTime.now();
  var now = DateTime.now();
  var content = "";
  PickerDataWithIdModel? selectType;
  @override
  void initState() {
    super.initState();
    content = widget.content;
    selectType = widget.selectType;
  }
  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        CommonText(widget.title, fontWeight: FontWeight.w600, textSize: 20),
        Container(
          height: 10,
        ),
        CommonInput(
          text: content,
          hintText: widget.hint,
          onChanged: (str) {
            content = str;
          },
          isDense: true,
          keyboardType:  widget.keyboard,
          border: InputBorder.none,
          padding: EdgeInsets.zero,
          maxLines: 5,
        ).addToContainer(
            decoration: BoxDecoration(
                color: Colors.grey[200], borderRadius: 6.borderRadius),
            padding: EdgeInsets.all(8)),
        Container(
          height: 16,
        ),
        TitleValueWidget(
          '抄表时间',
          isRequired: true,
          hint: '选择抄表时间',
          text: selectTime.toDateStr(format),
          padding: EdgeInsets.zero,
        ).onTap(() {
          DatePicker.showDateTimePicker(context, currentTime: selectTime,
              onConfirm: (date) {
                setState(() {
                  selectTime = date!;
                });
              });
        }),
        SizedBox(
          height: 15,
        ),
        TitleValueWidget('抄表类型',
            isRequired: true,
            hint: '选择抄表类型',
            text: selectType?.name??"",
            padding: EdgeInsets.zero)
            .onTap(() {
          DataPickerView(context, widget.typeList, currentFirstData: selectType,
              onSubmitListener: (p1, p2, p3) {
                setState(() {
                  selectType = widget.typeList[p1];
                });
              }).show();
        }),
        SizedBox(
          height: 15,
        ),
        TitleValueWidget('操作时间',
            isRequired: false,
            hint: '',
            text: now.toDateStr(format),
            showArrow: false,
            padding: EdgeInsets.zero),
        SizedBox(
          height: 15,
        ),
        ElevatedButton(
          onPressed: () {
            if (widget.onSubmit != null) {
              Navigator.pop(context);
              var data = MeterReadingData(
                  reading: content,
                  readTime: selectTime.toDateStr(format),
                  readingType: selectType);
             if(widget.onSubmit!=null){
               widget.onSubmit!(data);
             }
            }
          },
          child: CommonText(widget.btnText, textColor: Colors.white),
          style: ButtonStyle(
              elevation: WidgetStateProperty.all(0),
              shape: WidgetStateProperty.all(StadiumBorder()),
              minimumSize:
              WidgetStateProperty.all(Size(double.infinity, 45))),
        )
      ],
    ).addToContainer(
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius:
            BorderRadius.only(topRight: 10.radius, topLeft: 10.radius)),
        padding: EdgeInsets.all(16));
  }

}

void showMeterReadingBottomInput(BuildContext context,
    {String title = "请输入",
    String hint = "请输入",
    String btnText = "确定",
    String content = "",
    CommonInputType keyboard = CommonInputType.multiline,
    dynamic selectType,
    Function(MeterReadingData reading)? onSubmit}) {
  showModalBottomSheet(
      context: context,
      builder: (context) {
        return SingleChildScrollView(
          child: MeterReadingBottomInputWidget(
            title: title,
            hint: hint,
            btnText: btnText,
            onSubmit: onSubmit,
            content: content,
            keyboard: keyboard,
            typeId:selectType,
          ),
          padding:
              EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
        );
      },
      backgroundColor: Colors.transparent,
      isScrollControlled: true);
}

class MeterReadingData {
  String? reading;
  String? readTime;
  PickerDataWithIdModel? readingType;

  MeterReadingData({this.reading, this.readTime, this.readingType});
}
